﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.WeChatComponent.WeChatCustomMenu;
using Dapper;

namespace BCData.WeChatComponent.WeChatCustomMenu
{
    /// <summary>
    /// 微信自定义菜单数据处理
    /// </summary>
    public class WeChatCustomMenuData : IWeChatCustomMenuData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT WeChatCustomMenuId,Name,Level,Type,Url,ClickKey,MediaId,Appid,PagePath,ParentMenuId FROM WeChatCustomMenu";

        public WeChatCustomMenuData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public WeChatCustomMenuEntity Add(WeChatCustomMenuEntity weChatCustomMenuEntity)
        {
            string sql = @"INSERT INTO WeChatCustomMenu(Name,Level,Type,Url,ClickKey,MediaId,Appid,PagePath,ParentMenuId)
                                    VALUES(?Name,?Level,?Type,?Url,?ClickKey,?MediaId,?Appid,?PagePath,?ParentMenuId);
                            {0} WHERE WeChatCustomMenuId=LAST_INSERT_ID();";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Name", weChatCustomMenuEntity.Name, DbType.AnsiString, size: 10);
            parameters.Add("?Level", weChatCustomMenuEntity.Level, DbType.Int32, size: 10);
            parameters.Add("?Type", weChatCustomMenuEntity.Type.ToString(), DbType.AnsiString);
            parameters.Add("?Url", weChatCustomMenuEntity.Url, DbType.AnsiString, size: 1000);
            parameters.Add("?ClickKey", weChatCustomMenuEntity.ClickKey, DbType.AnsiString, size: 128);
            parameters.Add("?MediaId", weChatCustomMenuEntity.MediaId, DbType.AnsiString, size: 100);
            parameters.Add("?Appid", weChatCustomMenuEntity.Appid, DbType.AnsiString, size: 255);
            parameters.Add("?PagePath", weChatCustomMenuEntity.PagePath, DbType.AnsiString, size: 1000);
            parameters.Add("?ParentMenuId", weChatCustomMenuEntity.ParentMenuId, DbType.Int32, size: 10);
            return this.databaseContext.Master.QueryEntity<WeChatCustomMenuEntity>(string.Format(sql, QUERY), parameters, databaseContext.Transaction);
        }
        
        public bool CheckMenuExists(string name, int level)
        {
            string sql = @"SELECT 1 FROM WeChatCustomMenu WHERE Name=?Name AND Level=?Level;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Name", name, DbType.AnsiString, size: 10);
            parameters.Add("?Level", level, DbType.Int32, size: 10);
            object result = this.databaseContext.Slave.ExecuteScalar(sql, parameters);
            return result != null;
        }

        public void Delete(int weChatCustomMenuId)
        {
            string sql = @"DELETE FROM WeChatCustomMenu WHERE WeChatCustomMenuId=?WeChatCustomMenuId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatCustomMenuId", weChatCustomMenuId, DbType.Int32, size: 10);
            this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
        }

        public IEnumerable<WeChatCustomMenuEntity> GetList()
        {
            return this.databaseContext.Slave.Query<WeChatCustomMenuEntity>(QUERY);
        }

        public int GetMenuCount(int level,int? weChatCustomMenuId)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = @"SELECT COUNT(1) FROM WeChatCustomMenu WHERE Level=?Level";
            parameters.Add("?Level", level, DbType.Int32, size: 10);
            if (weChatCustomMenuId.HasValue&& weChatCustomMenuId.Value>0)
            {
                sql += " AND ParentMenuId=?WeChatCustomMenuId";
                parameters.Add("?WeChatCustomMenuId", weChatCustomMenuId, DbType.Int32, size: 10);
            }
            sql += ";";
            return this.databaseContext.Slave.ExecuteScalar<int>(sql, parameters);
        }

        public WeChatCustomMenuEntity Update(WeChatCustomMenuEntity weChatCustomMenuEntity)
        {
            string sql = @"UPDATE WeChatCustomMenu SET 
                                Name=?Name,
                                Level=?Level,
                                Type=?Type,
                                Url=?Url,
                                ClickKey=?ClickKey,
                                MediaId=?MediaId,
                                Appid=?Appid,
                                PagePath=?PagePath
                            WHERE WeChatCustomMenuId=?WeChatCustomMenuId;
                            {0} WHERE WeChatCustomMenuId=?WeChatCustomMenuId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatCustomMenuId", weChatCustomMenuEntity.WeChatCustomMenuId, DbType.Int32, size: 10);
            parameters.Add("?Name", weChatCustomMenuEntity.Name, DbType.AnsiString, size: 10);
            parameters.Add("?Level", weChatCustomMenuEntity.Level, DbType.Int32, size: 10);
            parameters.Add("?Type", weChatCustomMenuEntity.Type.ToString(), DbType.AnsiString);
            parameters.Add("?Url", weChatCustomMenuEntity.Url, DbType.AnsiString, size: 1000);
            parameters.Add("?ClickKey", weChatCustomMenuEntity.ClickKey, DbType.AnsiString, size: 128);
            parameters.Add("?MediaId", weChatCustomMenuEntity.MediaId, DbType.AnsiString, size: 100);
            parameters.Add("?Appid", weChatCustomMenuEntity.Appid, DbType.AnsiString, size: 255);
            parameters.Add("?PagePath", weChatCustomMenuEntity.PagePath, DbType.AnsiString, size: 1000);
            parameters.Add("?ParentMenuId", weChatCustomMenuEntity.ParentMenuId, DbType.Int32, size: 10);
            return this.databaseContext.Master.QueryEntity<WeChatCustomMenuEntity>(string.Format(sql, QUERY), parameters, databaseContext.Transaction);
        }

        public bool CheckMenuExists(int weChatCustomMenuId)
        {
            string sql = @"SELECT 1 FROM WeChatCustomMenu WHERE WeChatCustomMenuId=?WeChatCustomMenuId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatCustomMenuId", weChatCustomMenuId, DbType.Int32, size: 10);
            object result = this.databaseContext.Slave.ExecuteScalar(sql, parameters);
            return result != null;
        }

        public WeChatCustomMenuEntity Get(int weChatCustomMenuId)
        {
            string sql = @"{0} WHERE WeChatCustomMenuId=?WeChatCustomMenuId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatCustomMenuId", weChatCustomMenuId, DbType.Int32, size: 10);
            return this.databaseContext.Slave.QueryEntity<WeChatCustomMenuEntity>(string.Format(sql,QUERY), parameters);
        }

        public WeChatCustomMenuEntity Get(string name, int level)
        {
            string sql = @"{0} WHERE Name=?Name AND Level=?Level;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Name", name, DbType.AnsiString, size: 10);
            parameters.Add("?Level", level, DbType.Int32, size: 10);
            return this.databaseContext.Slave.QueryEntity<WeChatCustomMenuEntity>(string.Format(sql, QUERY), parameters);
        }
    }
}
